/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BandejaDeEntradaView.java
 *
 * Created on 16-ene-2011, 16:43:21
 */
package Vista.TareaRealizada;

import Modelo.Entidades.Alumno;
import Modelo.Entidades.RespuestaContestada;
import Modelo.Entidades.Tarea;
import Vista.SesionActual;
import Vista.Tarea.TareaView;
import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */
public class BandejaDeEntradaView extends javax.swing.JPanel {

    private TareaView tareaView;
    private TareaRealizadaViewImpl tareaRealizadaView;
    private TareaTableModel tareaTableModel;
    private Tarea tareaSeleccionada = null;
    private List<Tarea> tareas = null;

    /** Creates new form BandejaDeEntradaView */
    public BandejaDeEntradaView(TareaView tareaView, TareaRealizadaViewImpl tareaRealizadaView) {
        this.tareaView = tareaView;
        this.tareaRealizadaView = tareaRealizadaView;
        tareaTableModel = TareaTableModel.create();
        tareas = new ArrayList();

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablaTareas = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jRealizar = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tareas Pendientes de Realización", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jTablaTareas.setModel(tareaTableModel);
        jTablaTareas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablaTareasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTablaTareas);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jRealizar.setText("Realizar");
        jRealizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRealizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                    .addComponent(jRealizar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRealizar)
                .addContainerGap(209, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTablaTareasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTablaTareasMouseClicked
        TareaTableModel ttm = (TareaTableModel) this.jTablaTareas.getModel();
        Tarea tarea = ttm.getRow(this.jTablaTareas.getSelectedRow());
        tareaSeleccionada = tarea;
    }//GEN-LAST:event_jTablaTareasMouseClicked

    private void jRealizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRealizarActionPerformed
        new RealizarTareaView(this, tareaSeleccionada);
    }//GEN-LAST:event_jRealizarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jRealizar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTablaTareas;
    // End of variables declaration//GEN-END:variables

    public void refrescaVista() {
        tareas.clear();
        tareaSeleccionada = null;

        this.obtieneTareas();
        this.tareaTableModel.setTareas(tareas);
    }

    private void obtieneTareas() {
        String DNI = SesionActual.instancia().obtieneSesion().getUsuario().getDNI();
        List<Tarea> tareasSinFiltrar = this.tareaView.getModel().obtieneTareasByDNI(DNI);

        for (Tarea t : tareasSinFiltrar) {
            if ((this.tareaRealizadaView.getModel().isRealizada(DNI, t.getNombre())) == false) {
                tareas.add(t);
            }
        }
    }

    public void evaluacionFinalizada(List<RespuestaContestada> respuestasContestadas, Time duracion, Date fechaInicio) {
        this.tareaRealizadaView.fireNuevaTareaRealizadaGesture((Alumno) SesionActual.instancia().obtieneSesion().getUsuario(), tareaSeleccionada, duracion, fechaInicio, -1, respuestasContestadas);
    }

    void publicaNota(double nota) {
        if (nota >= 5) {
            try {
                //llamara a la animacion
                String animacion = "animacion/enhorabuena.swf";
                java.lang.Runtime.getRuntime().exec("player.exe " + animacion);
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }

        JOptionPane.showMessageDialog(null, "Calificación: " + nota);
    }
}
